import xlwings as xw
app = xw.App(visible=True, add_book=False)
workbook = app.books.open('F:\\python\\第4章\\成绩表.xlsx')
worksheet = workbook.sheets['Sheet1']
rng = worksheet.range('B2').expand('table')
for r in rng.rows:
    row_addr = r.get_address(row_absolute=False, column_absolute=False)
    row_formula = f'=SUM({row_addr})'
    cell = r.last_cell.offset(column_offset=1)
    cell.formula = row_formula
for c in rng.columns:
    col_addr = c.get_address(row_absolute=False, column_absolute=False)
    col_formula = f'=AVERAGE({col_addr})'
    cell = c.last_cell.offset(row_offset=1)
    cell.formula = col_formula
workbook.save('F:\\python\\第4章\\成绩表1.xlsx')
workbook.close()
app.quit()
